package com.study.zzedu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.DiscussItem;
import com.study.zzedu.ui.discussdetail.DiscussDetailActivity;
import com.study.zzedu.utils.AppConstant;

import java.util.List;

/**
 * Created by kiros on 2020/9/6.
 * 在线讨论adapter
 */
public class DiscussAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<DiscussItem> discussItems;
    private final static int HEAD_COUNT = 1;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;

    public DiscussAdapter(Context context, List<DiscussItem> discussItems) {
        this.context = context;
        this.discussItems = discussItems;
    }

    @Override
    public int getItemViewType(int position) {
        if (HEAD_COUNT != 0 && position == 0) { // 头部
            return TYPE_HEAD;
        } else
        {
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD){
            View itemView = LayoutInflater.from(context).inflate(R.layout.layout_discuss_head,parent,false);
            return new HeadHolder(itemView);
        }else
            {
            View itemView = LayoutInflater.from(context).inflate(R.layout.discuss_item, parent, false);
            return new ContentHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadHolder) { // 头部
            final HeadHolder mholder = (HeadHolder) holder;
            //final DiscussItem em = discussItems.get(position);
            mholder.tvName.setText(AppConstant.lessonName+"课程论坛");
        } else if (holder instanceof ContentHolder) { // 内容
                final ContentHolder mholder = (ContentHolder) holder;
                final DiscussItem em = discussItems.get(position);
                mholder.tv_title.setText(em.getTitle());
                //mholder.tv_author.setText(em.get);
                mholder.tv_views.setText(""+em.getViews());
                mholder.ll_discuss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AppConstant.contentID = em.getId();
                        view.getContext().startActivity(new Intent(view.getContext(), DiscussDetailActivity.class));
                    }
                });

        } else { // 尾部

        }
    }


    @Override
    public int getItemCount() {
        return discussItems.size();
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        public HeadHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_course_name);
        }
    }

    public class ContentHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_discuss;
        private TextView tv_title,tv_views,tv_author;

        public ContentHolder(View itemView) {
            super(itemView);
            ll_discuss = itemView.findViewById(R.id.ll_discuss_item);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_author = itemView.findViewById(R.id.tv_author);
            tv_views = itemView.findViewById(R.id.tv_views);
        }
    }
}